package com.filyar.htyd.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.filyar.htyd.common.Result;
import com.filyar.htyd.entity.Schedule;
import com.filyar.htyd.service.IScheduleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author filyar
 * @since 2024-08-29
 * @e-mail root@filyar.com
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Resource
    private IScheduleService scheduleService;

    private final String now = DateUtil.now();

    @GetMapping("/getPersonDayTypeShiftHistoryList")
    public Result getPersonDayTypeShiftHistoryList(@RequestParam Integer personId,
                                             @RequestParam Integer dayTypeId){
        return Result.success(scheduleService.list(new LambdaQueryWrapper<Schedule>()
                .eq(Schedule::getPersonId, personId)
                .eq(Schedule::getDayTypeId, dayTypeId)));
    }

    @GetMapping("/getPersonStatistics/{personId}")
    public Result getPersonStatistics(@PathVariable Integer personId) {
        return Result.success(scheduleService.getPersonStatistics(personId));
    }

    @GetMapping("/getPersonShiftHistoryList")
    public Result getPersonShiftHistoryList(@RequestParam Integer personId,
                                            @RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize){
        return Result.success(scheduleService.getPersonShiftHistoryList(personId, pageNum, pageSize));
    }

    @GetMapping("/shift/{date}")
    public Result shift(@PathVariable String date) {
        return Result.success(scheduleService.shift(date));
    }


    @DeleteMapping("/deleteSchedule/{date}")
    public Result deleteSchedule(@PathVariable String date) {
        scheduleService.deleteSchedule(date);
        return Result.success();
    }

    /**
     * 获取修改值班人员时的可以代替的人员list
     * @return
     */
    @GetMapping("/getPersonList")
    public Result getPersonList(@RequestParam Integer dayTypeId,
                                @RequestParam Integer gender) {
        return Result.success(scheduleService.getPersonList(dayTypeId,gender));
    }

    @GetMapping("/querySchedule")
    public Result querySchedule(@RequestParam String date){
        return Result.success(scheduleService.querySchedule(date));
    }

    @GetMapping("/generateSchedule")
    public Result generate(@RequestParam String date) {
        return Result.success(scheduleService.generate(date));
    }

    @PostMapping
    public Result save(@RequestBody Schedule schedule) {
        scheduleService.saveOrUpdate(schedule);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        scheduleService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        scheduleService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(scheduleService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        return Result.success(scheduleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }



}

